import React from 'react';
import { connect } from 'react-redux'
import { messages, getQueryUrlParam, deepFullCopy } from "share/common";
import customField from 'share/customField'
import constants from 'share/constants'

import NewExpenseReport from 'containers/expense-report/new-expense-report'
import ApprovingEdit from 'containers/approve/approving-edit'
import SlideFrame from 'components/slide-frame'
import Condition from 'components/condition'
const { If } = Condition;

class ExpenseReportFields extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      showEditExpenseReport: false,
    }
  }

  //获取单据状态
  getStatus = () => {
    const { info } = this.props;
    let status = constants.documentStatus;
    let result = {};
    status.map(item => {
      if (item.value === String(info.status) || item.value === String(info.status * 10000 + info.rejectType)) {
        result = item;
      }
    });
    return result;
  };

  handleClose = (value) => {
    this.setState({ showEditExpenseReport: false });
    this.props.afterSaveByMyself(value)
  };

  //处理一人多岗情况下，申请人的信息
  dealApplicantInfo = () => {
    const { applicant, jobInfo } = this.props;
    let applicantInfo = deepFullCopy(applicant || {});
    jobInfo && jobInfo.departmentOID && (applicantInfo.departmentOID = jobInfo.departmentOID);
    jobInfo && jobInfo.companyOID && (applicantInfo.companyOID = jobInfo.companyOID);
    return applicantInfo;
  };

  render() {
    const { loading, readOnly, info, custFormValues, form, propsObj, pageFrom, expenseReportInvoices, jobInfo } = this.props;
    const { showEditExpenseReport } = this.state;
    let isApprovePage = propsObj.location.pathname.indexOf('approve-expense-report-detail') > -1;
    let isAuditEditable= propsObj.params.tab === 'prending_audit' && this.checkFunctionProfiles('finance.audit.edit.enable', [true]);
    let signName = customField.getSignNameForSubmitter(form && form.customFormPropertyMap);
    let expenseReportStatus = info ? this.getStatus() : {};
    return (
      <div className="tab-container direct-attachment">
        <h3 className="sub-header-title">
          {messages('expense-parent-report.key34'/*报销单详情*/)}
          <If value={!loading && !readOnly}>
            <a className="edit" onClick={() => this.setState({showEditExpenseReport: true})}>
              {messages('common.edit')/*编辑*/}
            </a>
          </If>
          <If value={isApprovePage || isAuditEditable}>
            <ApprovingEdit
              approvalOID={getQueryUrlParam('approverOID')}
              entityOID={info.expenseReportOID}
              formOID={info.formOID}
              entityType={1002}
              custFormValues={custFormValues}
              handleAfterSave={this.props.afterSaveByApprover}
            />
          </If>
        </h3>
        {customField.renderFields(custFormValues, info, this.dealApplicantInfo(), pageFrom, signName)}

        <If value={!loading && expenseReportStatus.operate === 'edit'}>
          <SlideFrame
            show={showEditExpenseReport}
            content={NewExpenseReport}
            title={messages('expense-parent-report.key49'/*编辑报销单*/)}
            params={{
              expenseReport: info,
              formDetail: form,
              expenseReportInvoices,
              jobInfo,
              driverId: info.formCode === 'BX006' && info.carPerson && info.carPerson.id
            }}
            onClose={() => this.setState({ showEditExpenseReport: false })}
            afterClose={this.handleClose}
            hasFooter={false}
          />
        </If>
      </div>
    )
  }
}

ExpenseReportFields.propTypes = {
  loading: React.PropTypes.bool,
  readOnly: React.PropTypes.bool,
  propsObj: React.PropTypes.object,
  info: React.PropTypes.object, //报销单详情
  jobInfo: React.PropTypes.object, //岗位信息
  applicant: React.PropTypes.object,
  custFormValues: React.PropTypes.array,
  form: React.PropTypes.object,
  pageFrom: React.PropTypes.string,
  expenseReportInvoices: React.PropTypes.array, //费用列表
  afterSaveByApprover: React.PropTypes.func, //审批、审核页面详情编辑保存后
  afterSaveByMyself: React.PropTypes.func, //编辑中的单据详情编辑保存后
};

function mapStateToProps(state) {
  return {
    company: state.login.company
  }
}

export default connect(mapStateToProps)(ExpenseReportFields)
